package com.app.v1;

import com.bean.ResponseBean;
import com.pojo.Content;
import com.pojo.Module;
import com.service.ContentService;
import com.service.ModuleService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/h5/content")
public class AppContentAction {

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private ContentService contentService;

    /**
     * 获取菜单下所有模块和内容
     * @param parentName
     * @return
     */
    @RequestMapping("/getModuleContent")
    public ResponseBean getModuleContent (String parentName) {
        if (ValidateHelper.isEmptyString(parentName)) {
            return ResponseBean.error("父级菜单为空");
        }
        List resultList = new ArrayList();
        List<Module> moduleList = moduleService.getModuleListByParentName(parentName);
        if("teacher".equals(parentName)){        // 如果是查询师资内容
            String msg="";
            if (ValidateHelper.isNotEmptyCollection(moduleList)) {
                msg = moduleList.get(0).getIntroduce();
                for (Module module : moduleList) {
                    Map<String, Object> resultMap = new HashMap<>();
                    List<Content> contentList = contentService.getContentByMId(module.getId());
                    resultMap.put("key",module.getName());
                    resultMap.put("value",contentList);
                    resultList.add(resultMap);
                }
                Map map = new HashMap();
                map.put("info",msg);
                resultList.add(map);
            }
            return new ResponseBean(200, msg, resultList);
        }else  if("huanjing".equals(parentName)){        // 如果是查询环境内容
            String msg="";
            if (ValidateHelper.isNotEmptyCollection(moduleList)) {
                msg = moduleList.get(0).getIntroduce();
                for (Module module : moduleList) {
                    Map<String, Object> resultMap = new HashMap<>();
                    List<Content> contentList = contentService.getContentByMId(module.getId());
                    resultMap.put("key",module.getName());
                    resultMap.put("value",contentList);
                    resultList.add(resultMap);
                }
                Map map = new HashMap();
                map.put("info",msg);
                resultList.add(map);
            }
            return new ResponseBean(200, msg, resultList);
        }
        else   if("jiuye".equals(parentName)){        // 如果是就业内容
            String msg="";
            if (ValidateHelper.isNotEmptyCollection(moduleList)) {
                msg = moduleList.get(0).getIntroduce();
                for (Module module : moduleList) {
                    Map<String, Object> resultMap = new HashMap<>();
                    List<Content> contentList = contentService.getContentByMId(module.getId());
                    resultMap.put("key",module.getName());
                    resultMap.put("value",contentList);
                    resultList.add(resultMap);
                }
                Map map = new HashMap();
                map.put("info",msg);
                resultList.add(map);
            }
            return new ResponseBean(200, "查询成功", resultList);
        }  {                 // 如果是查询其他内容
            if (ValidateHelper.isNotEmptyCollection(moduleList)) {
                for (Module module : moduleList) {
                    Map<String, Object> resultMap = new HashMap<>();
                    List<Content> contentList = contentService.getContentByMId(module.getId());
                    resultMap.put("key",module.getName());
                    resultMap.put("value",contentList);
                    resultList.add(resultMap);
                }
            }
            return new ResponseBean(200, "成功", resultList);
        }

    }
}
